package com.csii.activiti.server.sign.verify;

import cn.hutool.crypto.digest.HMac;
import cn.hutool.crypto.digest.HmacAlgorithm;
import com.alibaba.fastjson.JSONObject;

import java.util.TreeMap;

public class SignVerifyUtils {

    private static final String SIGN_KEY_COLUMN = "callbackSign";

    public static boolean verify(Object data,String signKey){
        if (data == null){
            return false;
        }
        try {
            HMac mac = new HMac(HmacAlgorithm.HmacMD5, signKey.getBytes("utf-8"));
            JSONObject jsonObject = (JSONObject) JSONObject.toJSON(data);
            String callbackSign = (String) jsonObject.remove(SIGN_KEY_COLUMN);
            JSONObject orderJsonObject = new JSONObject(new TreeMap());
            orderJsonObject.putAll(jsonObject);
            return mac.digestHex(orderJsonObject.toJSONString()).equals(callbackSign);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}
